package com.ddstation.threeDay.controller;

import com.ddstation.common.util.DateUtil;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.exam.entity.DtHealthManageInstitutionGroupEntity;
import com.ddstation.exchange.service.DtHealthManageInstitutionGroupService;
import com.ddstation.sewage.service.SewageService;
import com.ddstation.threeDay.model.ThreeDayDetailParaModel;
import com.ddstation.threeDay.model.ThreeDayResModel;
import com.ddstation.threeDay.service.ThreeDayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Lazy
public class ThreeDayController {
    /**
     * 数据查询接口，单个机构系统
     */
    @RequestMapping(value = "invs_threeDay.do/load_detail", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> loadRemoteListEvent(
            @RequestBody ThreeDayDetailParaModel _in,
            HttpSession session) {
        SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
        Map<String,Object> ret=new HashMap<>();
        ret.put("errcode",-1);
        ret.put("errMsg","系统出错，请稍后再试");

        // 访问权限check(防止异常调用)
        if ( !sewageService.checkAccessAuthority(loginData.getInstitution().getId(),_in.getInstitutionId()) ){
            ret.put("errMsg", "没有访问权限！");
            return ret;
        }
        // 日期check
        if ( StringUtils.isBlank(_in.getDtFrom())
                && StringUtils.isBlank(_in.getDtTo())
                && StringUtils.isBlank(_in.getDateYm())){
            ret.put("errMsg", "没有输入查询日期！");
            return ret;
        }

        // 查询年月进来的情况【即页面调用时】
        if ( StringUtils.isNotBlank(_in.getDateYm())   ){
            String[] beginEndDate = DateUtil.getBeginEndDate(_in.getDateYm());
            if ( beginEndDate == null ){
                ret.put("errMsg", "系统出错，传入参数：年月错误！");
                return ret;
            }
            _in.setDtFrom(beginEndDate[0]);
            _in.setDtTo(beginEndDate[1]);
        }
//        _in.setBusinessSubType(601);
        //查询机构全部子ID
        DtHealthManageInstitutionGroupEntity entity = new DtHealthManageInstitutionGroupEntity();
        entity.setParentInstitutionId(_in.getInstitutionId());
        List<DtHealthManageInstitutionGroupEntity> dtHealthManageInstitutionGroupList = dtHealthManageInstitutionGroupService.select(entity);
        //设置机构ID集合，必定存在父机构ID
        List<String> institutionIdList = new ArrayList<>();
        institutionIdList.add(_in.getInstitutionId());
        if(dtHealthManageInstitutionGroupList != null && dtHealthManageInstitutionGroupList.size() > 0){
            //存在子ID则全部加入in条件查询
            for(DtHealthManageInstitutionGroupEntity dtHealthManageInstitutionGroup:dtHealthManageInstitutionGroupList){
                institutionIdList.add(dtHealthManageInstitutionGroup.getChildInstitutionId());
            }
        }
        _in.setInstitutionIdList(institutionIdList);
        //去除原机构ID条件
        _in.setInstitutionId(null);
        List<ThreeDayResModel> datalist = threeDayService.searchDataList(_in);
        //替换展示 机构/科室 名称
        for(ThreeDayResModel data:datalist){
            if(institutionIdList.get(0).equals(data.getInstitutionId())){
                data.setInstitutionShortName(data.getInstitutionName());
            }
        }

        ret.put("datalist", datalist);

        ret.put("errcode", 0);
        ret.put("errMsg", "");
        return ret;
    }
    @Inject
    private SewageService sewageService;
    @Inject private DtHealthManageInstitutionGroupService dtHealthManageInstitutionGroupService;
    @Inject private ThreeDayService threeDayService;
}
